import mq_data_client as mq
import Core.Gadget as Gadget
import datetime
import pandas as pd


# 000623  2017-11-16
def Download_MutualFund(database):
    endDate = datetime.date(2020,1,1)
    #
    count = 0
    instruments = database.Find("Instruments", "MutualFund")
    for instrument in instruments:
        symbol = instrument["Symbol"]
        #
        datetime1 = instrument["DateTime1"].date()
        datetime2 = instrument["DateTime2"].date()
        if endDate < datetime2:
            datetime2 = endDate
        #
        documents = database.Find("DailyBar", "MutualFund", {"Symbol": symbol})
        if len(documents) == 0:
            print(symbol, "No Data")
            continue

        barDateTime1 = documents[0]["Date"]
        barDateTime2 = documents[-1]["Date"]

        diff = (datetime2 - barDateTime2).days
        if diff > 20:
            print(symbol, "Possible missing", diff)
            # a = mq.get_fund_value(code=symbol, count=10, exchange="CN")
            count += 1
    #
    print("Total Missing", count)


def TestDataDiff(database):
    #
    symbol = "000001.OF"
    instruments = database.Find("Instruments", "MutualFund", {"Symbol": symbol})
    instrument = instruments[0]
    #
    datetime1 = instrument["DateTime1"].date()
    datetime2 = instrument["DateTime2"].date()
    #
    df2 = mq.get_fund_value(code=symbol, start_date=datetime1, end_date=datetime2, exchange="CN")
    df2.reset_index(inplace=True)
    df2["time"] = pd.to_datetime(df2["time"])
    print(df2.dtypes)
    #
    documents = database.Find("DailyBar", "MutualFund", {"Symbol": symbol})
    df = Gadget.DocumentsToDataFrame(documents, keep=["Date", "NetAssetValue", "NetAssetValue_Accumulated", "NetAssetValue_Adjusted"])
    df["Date"] = pd.to_datetime(df["Date"])
    print(df.dtypes)
    #
    df = pd.merge(df, df2, left_on="Date", right_on="time")

    print(df)
    df.to_csv("d://test.csv")


def TestData_Cmpleteness(database):
    endDate = datetime.date(2020,1,1)
    #
    count = 0
    instruments = database.Find("Instruments", "MutualFund")
    for instrument in instruments:
        symbol = instrument["Symbol"]
        df2 = mq.get_fund_value(code=symbol, count=10, exchange="CN")
        if len(df2) == 0:
            print(symbol, "Missing")
            count += 1
    #
    print("Missing", count)


if __name__ == '__main__':
    #
    # ---Connect to DataBase, Find Series 连接数据库---
    import Core.Config as Config
    config = Config.Config()
    database = config.DataBase("MySQL")
    realtime = config.RealTime()
    #
    # a = mq.get_stock_price(code='600519.SH', count=10)
    # print(a)

    # Download_MutualFund(database)

    # TestDataDiff(database)

    TestData_Cmpleteness(database)
